package com.sky.aspect;



import com.sky.anno.UserRdis;
import com.sky.result.Result;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Aspect
@Component
@Slf4j
public class AutoUserDishRdis {

    @Autowired
    private RedisTemplate redisTemplate;

    @Around("@annotation(com.sky.anno.UserRdis)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        UserRdis annotation = signature.getMethod().getAnnotation(UserRdis.class);
        String method = annotation.value();
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(method + joinPoint.getArgs()[0]);
        if(list != null && list.size() > 0 ){
            log.info("查询了redis缓存-----");
            return Result.success(list);
        }
        Result<List<DishVO>> proceed = (Result<List<DishVO>>) joinPoint.proceed();
        redisTemplate.opsForValue().set(method+joinPoint.getArgs()[0],proceed.getData());
        return joinPoint.proceed();

    }

}
